package org.zz.tool;

import android.os.Environment;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

public class ToolUnit {
    public ToolUnit() {
    }

    public static String getSDCardPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if(sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
            return sdDir.toString();
        } else {
            return null;
        }
    }

    public static Boolean isExist(String path) {
        File file = new File(path);
        return !file.exists()?Boolean.valueOf(false):Boolean.valueOf(true);
    }

    public static Boolean AddDirectory(String path) {
        File file = new File(path);
        if(!file.exists()) {
            file.mkdir();
        }

        return Boolean.valueOf(true);
    }

    public static void GetSubFolders(String strMainFolder, List<String> strSubFolders) {
        File[] files = (new File(strMainFolder)).listFiles();

        for(int i = 0; i < files.length; ++i) {
            File f = files[i];
            if(f.isDirectory()) {
                strSubFolders.add(f.getPath());
            }
        }

    }

    public static void GetSubFiles(String Path, String Extension, List<String> strSubFiles) {
        File[] files = (new File(Path)).listFiles();

        for(int i = 0; i < files.length; ++i) {
            File f = files[i];
            if(f.isFile() && f.getPath().substring(f.getPath().length() - Extension.length()).equals(Extension)) {
                strSubFiles.add(f.getPath());
            }
        }

    }

    public static int SaveData(String filepath, byte[] buffer, int size) {
        File f = new File(filepath);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(f);
        } catch (FileNotFoundException var8) {
            var8.printStackTrace();
            return -1;
        }

        try {
            fos.write(buffer, 0, size);
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        try {
            fos.close();
            return 0;
        } catch (IOException var6) {
            var6.printStackTrace();
            return -2;
        }
    }

    public static long getFileSizes(String filepath) {
        File f = new File(filepath);
        long s = 0L;
        if(f.exists()) {
            FileInputStream fis = null;

            try {
                fis = new FileInputStream(f);
            } catch (FileNotFoundException var7) {
                var7.printStackTrace();
            }

            try {
                s = (long)fis.available();
            } catch (IOException var6) {
                var6.printStackTrace();
            }

            return s;
        } else {
            return -1L;
        }
    }

    public static byte[] ReadData(String filepath) {
        File f = new File(filepath);
        if(!f.exists()) {
            return null;
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int)f.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(f));
                short e = 1024;
                byte[] buffer = new byte[e];
                boolean len = false;

                int len1;
                while(-1 != (len1 = in.read(buffer, 0, e))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var8 = bos.toByteArray();
                return var8;
            } catch (IOException var20) {
                var20.printStackTrace();
            } finally {
                try {
                    in.close();
                } catch (IOException var19) {
                    var19.printStackTrace();
                }

                try {
                    bos.close();
                } catch (IOException var18) {
                    var18.printStackTrace();
                }

            }

            return null;
        }
    }

    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        if(file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }

        return flag;
    }

    public static void AppendFile(String fileName, String content, int iPos) {
        RandomAccessFile randomFile = null;

        try {
            randomFile = new RandomAccessFile(fileName, "rw");
            long e = randomFile.length();
            if(iPos == 0) {
                e = 0L;
            } else if(iPos > 0) {
                e = (long)iPos;
            }

            randomFile.seek(e);
            randomFile.writeBytes(content);
        } catch (IOException var14) {
            var14.printStackTrace();
        } finally {
            if(randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException var13) {
                    var13.printStackTrace();
                }
            }

        }

    }

    public static void AppendFile_bk(String fileName, String content, int iPos) {
        RandomAccessFile randomFile = null;

        try {
            randomFile = new RandomAccessFile(fileName, "rw");
            long e = randomFile.length();
            if(iPos == 0) {
                e = 0L;
            } else if(iPos > 0) {
                e = (long)iPos;
            }

            randomFile.seek(e);
            int szlen = content.length();
            byte[] buffer = new byte[szlen];
            buffer = content.getBytes();
            randomFile.write(buffer);
        } catch (IOException var16) {
            var16.printStackTrace();
        } finally {
            if(randomFile != null) {
                try {
                    randomFile.close();
                } catch (IOException var15) {
                    var15.printStackTrace();
                }
            }

        }

    }
}
